<?php
/*
 * @Description: file content
 * @FilePath: \auxface_api\app\certificate\controller\admin\v1\machineDigitization\Type.php
 */

declare(strict_types=1);

namespace app\certificate\controller\admin\v1\flow;

use think\facade\Db;
use think\Request;

class Index
{
    /**
     * @Description: 
     * @param {Request} $request
     */
    public function getList(Request $request)
    {
        $data  = $request->data;

        $where = [
            ['projectCode', '=', $request->prjCode],
            ['isDelete', '=', 0]
        ];

        if (!empty($data['name'])) array_push($where, ['name', 'like', $data['name'] . "%"]);

        $list = DB::table('certificate_flow')
            ->where($where)
            ->field(['name', 'remark', 'inDate', 'uuid'])
            ->order('id desc')
            ->select();

        $count = DB::table('certificate_flow')
            ->where($where)
            ->count();

        $data = compact('list', 'count');

        return result(SUCCESS, $data);
    }
    /**
     * @Description: 
     * @param {Request} $request
     */
    public function create(Request $request)
    {
        $data = $request->data;

        $insert =  [
            'uuid' => createUuid(),
            'projectCode' => $request->prjCode,
            'corpCode' => $request->corpCode,
            'name' => $data['name'],
            'remark' => $data['remark'] ?? '',
        ];
        DB::table('certificate_flow')->insert($insert);

        return result(SUCCESS, ['uuid' => $insert['uuid'], 'inDate' => date('Y-m-d H:i:s')], '添加成功!');
    }

    /**
     * @Description: 修改
     */
    public function update(Request $request)
    {
        $data = $request->data;
        $uuid = $data['uuid'];

        $cehck = DB::table('certificate_flow')->where([
            ['uuid', '<>', $uuid],
            ['name', '=', $data['name']],
            ['isDelete', '=', 0],
        ])->value('id');
        if ($cehck) return result(FAIL, '', '名称已存在,无法修改!');
        $updateData = [
            'name' => $data['name'],
            'remark' => $data['remark'] ?? '',
        ];
        DB::table('certificate_flow')->where('uuid', $uuid)->update($updateData);
        return result(SUCCESS, '', '更改成功');
    }

    public function destroy(Request $request)
    {
        $data = $request->data;
        DB::table('certificate_flow')->where('uuid', $data['uuid'])->update(['isDelete' => 1]);
        //删除关联的表单数据
        DB::table('certificate_flowForm')->where('flowUuid', $data['uuid'])->update(['isDelete' => 1]);
        //删除关联的附表数据
        DB::table('certificate_subForm')->where('flowUuid', $data['uuid'])->update(['flowUuid' => '']);
        //删除类型关联数据
        DB::table('certificate_machineRiskType')->where('flowUuid', $data['uuid'])->update(['flowUuid' => '']);
        return result(SUCCESS, '', '删除成功');
    }

     /**
     * @Description: 获取数据
     * @param {Request} $request
     */
    public function getDetail(Request $request)
    {
        $data = $request->data;
        $uuid = $data['uuid'];

        $data = DB::table('certificate_flow')->where('uuid', $uuid)->where('isDelete', 0)->field(['inDate', 'name', 'remark', 'uuid'])->find();
        if(!$data)  return result(FAIL,'','数据不存在');

        return result(SUCCESS, $data, '');
    }

}
